package com.example.studyproject.advice;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * @program: StudyProject
 * @description:
 * @author: kangwei
 * @create: 2024-08-14 10:59
 **/
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Response<T> implements Serializable {


    /**
     * 响应data
     */
    private T data;
    /**
     * 响应状态 true-成功 false 失败
     */
    private Boolean success;
    /**
     * 响应码值
     */
    private String code;
    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应成功：包含data
     *
     * @param data
     * @return
     */
    public static <T> Response<T> success(T data) {
        return new Response<>(data, true, "0000", "成功");
    }

    /**
     * 响应成功，包含data message
     *
     * @param data
     * @param msg
     * @return
     */
    public static <T> Response<T> success(T data, String msg) {
        return new Response<>(data, true, "0000", msg);
    }

    /**
     * 响应成功
     *
     * @return
     */
    public static <T> Response<T> success() {
        return new Response<>(true, "0000", "成功");
    }

    /**
     * 失败，包含data
     *
     * @param data
     * @return
     */
    public static <T> Response<T> error(T data) {
        return new Response<>(data, false, "9999", "失败");
    }

    /**
     * 失败
     *
     * @param <T>
     * @return
     */
    public static <T> Response<T> error() {
        return new Response<>(false, "9999", "失败");
    }

    public Response(Boolean success, String code, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }
}
